var Ajax = new Object();
Ajax.isProcessing = true;

Ajax.call = function(method,url,query,callback,activate) {
	this.isProcessing = true;
	this.callbackMethod = callback;
	this.activate = activate;
	this.request = Ajax.getRequest();
	this.request.onreadystatechange = function() {Ajax.handleReadyStateChange();};
	if (method.toLowerCase() == "get") url = url + "?" + query;
	this.request.open(method, url, true);
    this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    //document.write(query);
    this.request.send(query); 
}

Ajax.getRequest = function () {
	var request;
	try { 
		//Opera 8.0+, Firefox, Safari
		request = new XMLHttpRequest();
	} catch (e) {
		//Internet Explorer requests
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				//something really went wrong
				alert("Duh! your browser broke!");
				return false;
			}
		}
	}
	return request;
}

Ajax.handleReadyStateChange = function(_id) {
	switch(this.request.readyState) {
		case 1: break;
		case 2: break;
		case 3: break;
		case 4:
				this.isProcessing = false;
				if (this.activate) {
					activate();
				}
				this.callbackMethod(this.request);
	}
}

Ajax.cancelRequest = function() {
	if (this.request === undefined) return true;
	else {
		this.request.onreadystatechange = function() {}; //empty function
		this.request.abort();
	}
}

var AjaxFormPost = new Object();
AjaxFormPost.send = function(form,callback) {
	var query = AjaxFormPost.buildQuery(form);
	Ajax.call(form.method,form.action,query,callback,true);
}

AjaxFormPost.buildQuery = function(form) {
	var query = "";
	for(var i=0; i< form.elements.length; i++) {
		if (i > 0) query+= "&";
		query+= form.elements[i].name;
		query+="=";
		query+=escape(form.elements[i].value);
	}
	return query;
}

var AjaxPaginatorHelper = new Object();
AjaxPaginatorHelper.updateParams = function(params) {
	this.params = params;
}
AjaxPaginatorHelper.store = function(url,params,callback) {
	this.url = url;
	this.params = params;
	this.callback = callback;
}
AjaxPaginatorHelper.send = function(pageNo) {
	var query = AjaxPaginatorHelper.buildQuery(pageNo);
	Ajax.call("POST",this.url,query,this.callback,false);
}

AjaxPaginatorHelper.buildQuery = function(pageNo) {
	var query = null;
	for (var key in this.params) {
		if (query != null) {
			query+= "&" + key + "=" + this.params[key];
		} else {
			query= key + "=" + this.params[key];
		}
	}
	if (query != null) {
		query+= "&pageNo=" + pageNo;
	} else {
		query= "pageNo=" + pageNo;
	}
	return query;
}